package com.boot.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created  on 18-6-28 下午5:04.
 * 作者: LiuLiHao
 * 说明:
 */
@Aspect
@Component
public class HttpAspect {

	private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
	@Pointcut("execution(public * com.boot.controller.*.*(..))")
	public void log(){
		System.out.println("HttpAspect");
	}
	@Before("log()")
	public void doBefore(JoinPoint joinPoint){
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();

		logger.info("url={}",request.getRequestURL());
		logger.info("method={}",request.getMethod());
		logger.info("ip={}",request.getLocalAddr());
		logger.info("calss_method={}",joinPoint.getSignature().getDeclaringTypeName()+
				"."+joinPoint.getSignature().getName());
		logger.info("args={}",joinPoint.getArgs());
//		System.out.println("111");
	}
	@Before("log()")
	public void doAfter(){
//		System.out.println("222");
	}

	@AfterReturning(returning = "object",pointcut="log()")
	public void doAfterRunning(Object  object){

//		logger.info("response{}",object.toString());
//		System.out.println("222");


	}
}
